package com.g24.partychecker;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Feesten extends ListActivity
{	
	WebDBAdapter webdb;
	DBASettings mDbSettings; 
	private String REGIOS;
	private long ROWID = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		mDbSettings = new DBASettings(this);
		mDbSettings.open();
		
		Cursor c = mDbSettings.fetchSetting(ROWID);
        startManagingCursor(c);
        c.moveToFirst();
        
        int telRij = c.getCount();
        if(telRij > 0)
        {		    
        	REGIOS = c.getString(2);
        	System.out.println("REGIOS = " + REGIOS);
        }
        else
        {
        	REGIOS = "Rotterdam";
        }
		
		//parse json data
	    JSONArray jArray;
		try 
		{
			webdb = new WebDBAdapter();
			String adata = webdb.getAllData(REGIOS);
			jArray = new JSONArray(adata);
			ArrayList<String> myArrN = new ArrayList<String>();
			ArrayList<String> myArrD = new ArrayList<String>();
		    for(int i=0;i<jArray.length();i++)
		    {
		    	JSONObject json_data = jArray.getJSONObject(i);	    	
		    	String abc = json_data.getString("naam");
		    	String def = json_data.getString("datum");
		    	myArrN.add(abc);
		    	myArrD.add(def);
		    }
		    
		    setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, myArrN));

			final ListView lv1 = getListView();
		  	lv1.setTextFilterEnabled(true);

		  	lv1.setOnItemClickListener(new OnItemClickListener() 
		  	{
		  		public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
		  		{
		  		    String str = lv1.getItemAtPosition(position).toString();
		  		    
		  		    System.out.println(str);

		  			Intent i = new  Intent();
		    		i.putExtra("FEESTID", str);
		    		i.setClassName("com.g24.partychecker", "com.g24.partychecker.Feestinfo");
		            startActivity(i);
		  		}
		  	});
		} 
		
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}